package com.liang.book.controller;

import com.liang.book.model.AddBuyBook;
import com.liang.book.model.BookInfo;
import com.liang.book.model.Purchase;
import com.liang.book.model.UserInfo;
import com.liang.book.service.BuyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/buy")
@RestController
public class BuyerController {
  @Autowired
    private BuyService buyService;



  @PostMapping("/insertPurchaseDetail")
    public Boolean insertPurchaseDetail( Purchase purchase) {
      System.out.println("purchase:"+purchase);
        log.info("insertPurchase.buyerName: {}", purchase);
        if( purchase == null ){
            log.warn(" buyerName不能为空");
            return false;
        }
        try {
            Integer result = buyService.insertPurchaseDetail(purchase);
            if(result > 0) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("insertPurchase error,e :{}",e);
            return false;
        }
    }

    @RequestMapping("/addBuyBook")
    public Boolean addBuyBook(@RequestBody AddBuyBook addBuyBook) {
        System.out.println("AddBuyBook:"+addBuyBook);
        log.info("addBook {}", addBuyBook);
        if( addBuyBook == null ){
            log.warn(" addBuyBook不能为空");
            return false;
        }
        try {
            Integer result = buyService.addBuyBook(addBuyBook);
            if(result > 0) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("insertPurchase error,e :{}",e);
            return false;
        }
    }


    @RequestMapping("/selectPurchaseDetail")
    public Purchase selectPurchase(Integer id) {
      log.info("selectPurchase.id :" ,id);
          Purchase purchase = buyService.selectPurchaseDetail(id);
          return purchase;
    }

    @RequestMapping("/selectAddBook")
    public List<AddBuyBook> selectAddBook(String buyerName) {
        log.info("selectPurchase.buyerName :" ,buyerName);
        List<AddBuyBook> addBuyBook = buyService.selectAddBook(buyerName);
        return addBuyBook;
    }

    @RequestMapping("/selectPurchaseList")
    public List<Purchase> selectPurchaseList(String buyerName) {
        log.info("selectPurchase.buyerName :" ,buyerName);
        List<Purchase> purchases = buyService.selectPurchaseList(buyerName);
        return purchases;
    }

    @RequestMapping ("/insertPurchase")
    public Boolean insertPurchase(Purchase purchase) {
        System.out.println("purchase:"+purchase);
        log.info("insertPurchase.buyerName: {}", purchase);
        if( purchase == null ){
            log.warn(" buyerName不能为空");
            return false;
        }
        try {
            Integer result = buyService.insertPurchase(purchase);
            if(result > 0) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("insertPurchase error,e :{}",e);
            return false;
        }
    }
}
